package com.example.zhengchaojun.arcgis.basicesTest;

import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.data.VectorTileCache;
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.ArcGISVectorTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.RasterLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.view.LayerViewStateChangedEvent;
import com.esri.arcgisruntime.mapping.view.LayerViewStateChangedListener;
import com.esri.arcgisruntime.raster.Raster;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.map.BaseArcgisMapActivity;
import com.example.zhengchaojun.arcgis.util.FileUtils;

import java.io.File;
import java.util.List;

/**
 * 类描述：shp,tif,tpk等数据
 * 作者：郑朝军 on 2018/5/25
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/25
 * 修改备注：
 */
public abstract class BaseMapActivity extends BaseArcgisMapActivity
{
    public static String projectPath = Environment.getExternalStorageDirectory().getPath() + File.separator + "RuntimeViewer/Projects/01-测试工程示例";

    @Override
    public void initView()
    {
        super.initView();
        initBaseMapResource();
        initOperationalLayers();
        initBaseCSMapResource();
    }

    /**
     * 初始化基础底图资源
     */
    private void initBaseMapResource()
    {
        String path = "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineStreetPurplishBlue/MapServer";
        String name = "午夜蓝色中文不含兴趣点版中国基础地图";
        boolean visable = true;
        float opacity = 1f;
        initTiledCacheLayer(path, name, visable, opacity);
    }

    private void initBaseCSMapResource()
    {
        String path = "http://map.geoq.cn/arcgis/rest/services/ChinaOnlineCommunity/MapServer";
        String name = "彩色中文含兴趣点版中国基础地图";
        boolean visable = true;
        float opacity = 1f;
        initTiledCacheLayer(path, name, visable, opacity);
    }

    /**
     * 初始化业务图层
     */
    private void initOperationalLayers()
    {
        String path = getOperationalLayersPath();
        List<FileUtils.FileInfo> fileInfos = FileUtils.getFileListInfo(path, "shp");
        for (int i = 0; i < fileInfos.size(); i++)
        {
            FileUtils.FileInfo fileInfo = fileInfos.get(i);

            final ShapefileFeatureTable shapefileFeatureTable = new ShapefileFeatureTable(fileInfo.FilePath);
            shapefileFeatureTable.loadAsync();//异步方式读取文件
            shapefileFeatureTable.addDoneLoadingListener(new Runnable()
            {
                @Override
                public void run()
                {
                    //数据加载完毕后，添加到地图
                    FeatureLayer mainShapefileLayer = new FeatureLayer(shapefileFeatureTable);
                    mainShapefileLayer.setVisible(true);
                    m_mapView.getMap().getOperationalLayers().add(mainShapefileLayer);
                }
            });
        }
    }

    private void initTileLayer(String path, String name, boolean visable, float opacity)
    {//TPK
        TileCache tileCache = new TileCache(path);
        ArcGISTiledLayer localTiledLayer = new ArcGISTiledLayer(tileCache);
        localTiledLayer.setName(name);
        localTiledLayer.setVisible(visable);
        localTiledLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(localTiledLayer);
    }

    private void initRasterLayer(String path, String name, boolean visable, float opacity)
    {//Tiff
        Raster raster = new Raster(path);
        RasterLayer rasterLayer = new RasterLayer(raster);
        rasterLayer.setName(name);
        rasterLayer.setVisible(visable);
        rasterLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(rasterLayer);
    }

    private void initVectorTileLayer(String path, String name, boolean visable, float opacity)
    {//VTPK
        VectorTileCache vectorTileCache = new VectorTileCache(path);
        ArcGISVectorTiledLayer arcGISVectorTiledLayer = new ArcGISVectorTiledLayer(vectorTileCache);
        arcGISVectorTiledLayer.setName(name);
        arcGISVectorTiledLayer.setVisible(visable);
        arcGISVectorTiledLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(arcGISVectorTiledLayer);
    }

    private void initImageLayer(String url, String name, boolean visable, float opacity)
    {//在线动态图层
        ArcGISMapImageLayer dynamicMapServiceLayer = new ArcGISMapImageLayer(url);
        dynamicMapServiceLayer.setName(name);
        dynamicMapServiceLayer.setVisible(visable);
        dynamicMapServiceLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(dynamicMapServiceLayer);
    }

    private void initTiledLayer(String url, String name, boolean visable, float opacity)
    {//在线瓦片
        ArcGISTiledLayer tiledMapServiceLayer = new ArcGISTiledLayer(url);
        tiledMapServiceLayer.setName(name);
        tiledMapServiceLayer.setVisible(visable);
        tiledMapServiceLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(tiledMapServiceLayer);
    }

    private void initTiledCacheLayer(String path, String name, boolean visable, float opacity)
    {//在线瓦片
        TileCache tileCache = new TileCache(path);
        ArcGISTiledLayer localTiledLayer = new ArcGISTiledLayer(tileCache);
        localTiledLayer.setName(name);
        localTiledLayer.setVisible(visable);
        localTiledLayer.setOpacity(opacity);
        m_mapView.getMap().getBasemap().getBaseLayers().add(localTiledLayer);
    }

    /**
     * 获取基础底图路径
     *
     * @param path
     * @return
     */
    private String getBasemapPath(String path)
    {
        return projectPath + File.separator + "BaseMap" + File.separator + path;
    }

    /**
     * 获取基础底图路径
     *
     * @return
     */
    private String getOperationalLayersPath()
    {
        return projectPath + File.separator + "OperationalLayers";
    }

    public void showShort(String text)
    {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
}
